﻿using BestPracticesMVC.BL;
using BestPracticesMVC.BL.Helpers;
using BestPracticesMVC.Data.ORM;
using System;
using System.Linq;
using System.Web.Mvc;

namespace BestPracticesMVC.Controllers
{
    public partial class HomeController : Controller
    {
        private HomeBL LayerBL { get; set; }

        public HomeController(IUowData data)
        {
            this.LayerBL = new HomeBL(data);
        }

        public virtual ActionResult Index()
        {
            var result = this.LayerBL.GetAllAuthors();
            
            return View(result);
        }

        public virtual ActionResult About()
        {
            var result = this.LayerBL.CreateAuthor();

                       
            if (result.IsValid == false)
            {
                AddModelErrors(result);
                return View();
            }
           
            return RedirectToAction(T4MVC_HomeController.ActionNameConstants.Index);
        }
  
        private void AddModelErrors(Result result)
        {
            foreach (var error in result.Errors)
            {
                this.ModelState.AddModelError("existingUser", error);
            }
        }
    }
}